package com.zhipu.balance.ui.vm.home.calculate.rice

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordRice
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.CommonUtils

/**
 * 稻谷出糙率
 */
class RiceRougheningViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService){


    /**
     * 重量
     */
    var weighing : String? = null

    /**
     * 稻谷实体
     */
    var mRecordRice : RecordRice? = null

    /**
     * 当前的检查人员
     */
    var mPersonnel : Personnel? = null

    /**
     * 出糙率-试样质量
     */
    var roughSampleQuality = ObservableField("")
    /**
     * 完整糙米质量
     */
    var brownRiceQuality = ObservableField("")

    /**
     * 不完善糙米质量
     */
    var incompleteBrownRiceQuality = ObservableField("")
    /**
     * 出糙率-结果
     */
    var rougheningResults = ObservableField("")



    /**
     * 更新页面
     */
    fun updateRecordRice(){
        if(!TextUtils.isEmpty(mRecordRice?.rougheningRecorder)){
            //出糙率-试样质量
            roughSampleQuality.set(mRecordRice?.roughSampleQuality.toString())
            //完整糙米质量
            brownRiceQuality.set(mRecordRice?.brownRiceQuality.toString())
            //不完善糙米质量
            incompleteBrownRiceQuality.set(mRecordRice?.incompleteBrownRiceQuality.toString())
            //出糙率-结果
            rougheningResults.set(mRecordRice?.rougheningResults.toString())
        }
    }


    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        //出糙率-试样质量
        roughSampleQuality.set("")
        //完整糙米质量
        brownRiceQuality.set("")
        //不完善糙米质量
        incompleteBrownRiceQuality.set("")
        //出糙率-结果
        rougheningResults.set("")
    }




    /**
     * 出糙率-试样质量
     */
    fun BulkQuality(){
        roughSampleQuality.set(weighing)
        mRecordRice?.roughSampleQuality = roughSampleQuality.get()?.toDouble()
    }

    /**
     * 完整糙米质量
     */
    fun WholeBrownRiceQuality(){
        brownRiceQuality.set(weighing)
        mRecordRice?.brownRiceQuality = brownRiceQuality.get()?.toDouble()
    }

    /**
     * 不完善糙米质量
     */
    fun NotPerfectBrownRiceQuality(){
        incompleteBrownRiceQuality.set(weighing)
        mRecordRice?.incompleteBrownRiceQuality = incompleteBrownRiceQuality.get()?.toDouble()
    }


    /**
     * 计算
     */
    fun calculation(){
        /**
         * 计算出糙率
         */
        var CalculateRoughness = CommonUtils.formatDouble((mRecordRice!!.brownRiceQuality - 0.5 * mRecordRice!!.incompleteBrownRiceQuality) / mRecordRice!!.roughSampleQuality * 100)
        mRecordRice?.rougheningResults = CalculateRoughness
        rougheningResults.set(CalculateRoughness.toString())


        /**
         * 出糙率-试样质量
         */
        mRecordRice!!.rougheningRecorder = mPersonnel!!.name


        //出糙率-试样质量
        if(mRecordRice?.roughSampleQuality == 0.00){
            roughSampleQuality.set("0.00")
        }
        //完整糙米质量
        if(mRecordRice?.brownRiceQuality == 0.00){
            brownRiceQuality.set("0.00")
        }
        //不完善糙米质量
        if(mRecordRice?.incompleteBrownRiceQuality == 0.00){
            incompleteBrownRiceQuality.set("0.00")
        }

    }


}